package com.qf.smsplatfrom.service.impl;

import com.qf.smsplatfrom.constans.ResultCode;
import com.qf.smsplatfrom.events.UpdateMenuEvents;
import com.qf.smsplatfrom.exceptions.UpdateDataException;
import com.qf.smsplatfrom.mapper.MenuMapper;
import com.qf.smsplatfrom.pojo.CheckType;
import com.qf.smsplatfrom.pojo.TMenu;
import com.qf.smsplatfrom.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class MenuServiceImpl implements MenuService {

    private MenuMapper menuMapper;

    private ApplicationContext context;

    @Autowired
    public void setContext(ApplicationContext context) {
        this.context = context;
    }

    @Autowired
    public void setMenuMapper(MenuMapper menuMapper) {
        this.menuMapper = menuMapper;
    }

    @Override
    public void modifyByName(TMenu tMenu) {

        if (tMenu.isNull(CheckType.UPDATE)) {
            throw new UpdateDataException("传递的数据不能为空", ResultCode.DIRTY_NUM_ERROR);
        }

        menuMapper.updateMenu(tMenu);

        context.publishEvent(new UpdateMenuEvents());
    }
}
